package info.liuxing.okhttp.interceptor;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * LoggingInterceptor
 *
 * @author liuxing (SE)
 * @summary LoggingInterceptor
 * @Copyright (c) 2017, Lianjia Group All Rights Reserved.
 * @since 2017-11-30 16:15
 */
public class LoggingInterceptor implements Interceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggingInterceptor.class);

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        long t1 = System.nanoTime();
//        LOGGER.trace("发送请求: [{}]\n{}", request.url(), request.headers());

        Response response = chain.proceed(request);

        long t2 = System.nanoTime();
        LOGGER.trace("接收响应: [{}] 耗时 {}ms", response.request().url(), (t2 - t1) / 1000000d);

        return response;
    }
}